package com.wwj.auction.admin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wwj.auction.admin.entity.SellerAudit;
import com.wwj.auction.admin.entity.SellerAuditDetail;
import com.wwj.auction.admin.entity.Store;
import com.wwj.auction.admin.entity.UserInfo;
import com.wwj.auction.admin.entity.vo.SellerAuditDetailVo;
import com.wwj.auction.admin.enums.AuditStatus;
import com.wwj.auction.admin.enums.StoreStatus;
import com.wwj.auction.admin.mapper.SellerAuditDetailMapper;
import com.wwj.auction.admin.mapper.SellerAuditMapper;
import com.wwj.auction.admin.mapper.StoreMapper;
import com.wwj.auction.admin.mapper.UserInfoMapper;
import com.wwj.auction.admin.service.SellerAuditDetailService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wwj
 * @since 2021-08-27
 */
@Service
public class SellerAuditDetialServiceImpl extends ServiceImpl<SellerAuditDetailMapper, SellerAuditDetail> implements SellerAuditDetailService {

    @Resource
    private SellerAuditMapper sellerAuditMapper;
    @Resource
    private UserInfoMapper userInfoMapper;
    @Resource
    private StoreMapper storeMapper;

    @Override
    public void approval(SellerAuditDetailVo sellerAuditDetailVo) {
        // 封装数据
        SellerAuditDetail sellerAuditDetail = new SellerAuditDetail();
        // 设置用户id
        Integer sellerAuditId = sellerAuditDetailVo.getSellerId();
        sellerAuditDetail.setSellerAuditId(sellerAuditId);
        // 卖家申请是否通过
        Integer isPass = sellerAuditDetailVo.getStatus();
        // 通过id查询到用户的审核流程
        SellerAudit sellerAudit = sellerAuditMapper.selectById(sellerAuditId);
        if (isPass == 1) {
            // 若是卖家申请通过了，则需要将审核流程设置为审核通过
            sellerAudit.setStatus(AuditStatus.AUDIT_AGREE.getStatus());
            // 将用户的店铺状态设置为未开业
            UserInfo userInfo = userInfoMapper.selectById(sellerAudit.getUserId());
            userInfo.setStoreStatus(StoreStatus.NOT_OPENING.getStatus());
            // 更新
            userInfoMapper.updateById(userInfo);
            // 保存店铺信息
            Store store = new Store();
            store.setUserId(userInfo.getId());
            store.setStoreName(sellerAudit.getStoreName());
            // 服务评分、商品质量评分、物流服务评分默认为10.0
            store.setServiceScore(10.0f);
            store.setQualityScore(10.0f);
            store.setLogisticsScore(10.0f);
            // 设置开店时长
            store.setOpeningTime("0");
            // TODO 设置店铺负责人

            store.setPrincipalId("001");
            // 设置违规统计数量，初识为0
            store.setGetOut(0);
            // 保存
            storeMapper.insert(store);
        } else {
            // 否则需要将审核流程设置为审核不通过
            sellerAudit.setStatus(AuditStatus.AUDIT_DISAGREE.getStatus());
        }
        // 更新状态
        sellerAuditMapper.updateById(sellerAudit);
        sellerAuditDetail.setIsPass(isPass);
        // 基本信息是否正确
        if (sellerAuditDetailVo.getIsBasicInfoOk()) {
            sellerAuditDetail.setIsBasicInfo(1);
        } else {
            sellerAuditDetail.setIsBasicInfo(-1);
        }
        // 身份证照是否正确
        if (sellerAuditDetailVo.getIsCardImageOk()) {
            sellerAuditDetail.setIsIdCard(1);
        } else {
            sellerAuditDetail.setIsIdCard(-1);
        }
        // 联系人是否合法
        if (sellerAuditDetailVo.getIsBasicInfoOk()) {
            sellerAuditDetail.setIsContacts(1);
        } else {
            sellerAuditDetail.setIsContacts(-1);
        }
        // 备注
        sellerAuditDetail.setMemo(sellerAuditDetailVo.getMomo());
        // 保存
        baseMapper.insert(sellerAuditDetail);
    }
}
